import store from '../../store'

import serverInfo from './serverInfo'
import toolsHandle from './toolsHandle'
import previewHandle from './previewHandle'

import taskResultNotice from './taskResultNotice'
import circleNoticeHandle from './circleNoticeHandle'
import conversationHandle from './conversationHandle'


import { MessageBox, Message } from 'element-ui'

export default function handler(e) {
    let msg = JSON.parse(e.data)
    let msgType = msg.msgType
    switch (msgType) {
        // server 登陆结果 ok
        case 'DeviceAuthRsp':
            previewHandle.deviceAuthRspHandle(msg)
            break
        // sdk 微信列表 ok
        case 'GetWeChatsRsp':
            previewHandle.getWeChatsRspHandle(msg)
            break
        // server 微信登陆结果通知 ok
        case 'WeChatLoginNoticeResp':
            previewHandle.weChatLoginNoticeRespHandle(msg)
            break
        // 账号强制下线通知
        case 'AccountForceOfflineNotice':
            // console.log('账号强制下线通知')
            // console.log(JSON.parse(msg.message))
            break
        // 微信登陆通知
        case 'WeChatLoginNotice':
            // console.log('微信登陆通知')
            // console.log(JSON.parse(msg.message))
            break
        // 微信下线通知 ok
        case 'WeChatOfflineNotice':
            previewHandle.weChatOfflineNoticeHandle(msg)
            break
        // 微信上线通知 ok
        case 'WeChatOnlineNotice':
            previewHandle.weChatOnlineNoticeHandle(msg)
            break
        // ============================================================
        // 以下是wxtools页面 用到的回调
        // ============================================================
        // sdk 二维码 ok
        case 'PullWeChatQrCodeTaskResultNotice':
            toolsHandle.pullWeChatQrCodeTaskResultNoticeHandle(msg)
            break
        // sdk 新的朋友列表 ok
        case 'FriendAddReqListNotice':
            toolsHandle.friendAddReqListNoticeHandle(msg)
            break
        // sdk 清粉结果 ok
        case 'PostFriendDetectCountNotice':
            toolsHandle.postFriendDetectCountNoticeHandle(msg)
            break
        // server 拉取服务端上的自动任务 ok
        case 'AutoTaskDetailListResp':
            toolsHandle.autoTaskDetailListRespHandle(msg)
            break
        // server 更新服务端上的自动任务 ok
        case 'AutoTaskUpdateResp':
            toolsHandle.autoTaskUpdateRespHandle(msg)
            break
        // sdk 添加好友请求通知
        case 'FriendAddReqeustNotice':
            toolsHandle.friendAddReqeustNoticeHandle(msg)
            break
        // sdk 查询联系人结果
        case 'FindContactTaskResult':
            toolsHandle.findContactTaskResultNoticeHandle(msg)
            break
        // ============================================================
        // 以下是cirlces页面 用到的回调
        // ============================================================
        // 返回指定微信ID的朋友圈信息 ok
        case 'CirclePushNotice':
            circleNoticeHandle.circlePushNoticeHandle(msg)
            break
        // 删除朋友圈推送 ok
        case 'CircleDelNotice':
            circleNoticeHandle.circleDelNoticeHandle(msg)
            break
        // 新朋友圈推送 ok
        case 'CircleNewPublishNotice':
            circleNoticeHandle.circleNewPublishNoticeHandle(msg)
            break
        // 朋友圈点赞推送 ok
        case 'CircleLikeNotice':
            circleNoticeHandle.circleLikeNoticeHandle(msg)
            break
        // 朋友圈评论推送 ok
        case 'CircleCommentNotice':
            circleNoticeHandle.circleCommentNoticeHandle(msg)
            break
        // 发朋友圈的结果通知 ok
        case 'PostSNSNewsTaskResultNotice':
            circleNoticeHandle.postSNSNewsTaskResultNoticeHandle(msg)
            break
        // 朋友圈大图 ok
        case 'CircleDetailNotice':
            circleNoticeHandle.circleDetailNoticeHandle(msg)
            break
        // 朋友圈评论结果通知 ok
        case 'CircleCommentReplyTaskResultNotice':
            circleNoticeHandle.circleCommentReplyTaskResultNoticeHandle(msg)
            break
        // 朋友圈删除评论结果通知 ok
        case 'CircleCommentDeleteTaskResultNotice':
            circleNoticeHandle.circleCommentDeleteTaskResultNoticeHandle(msg)
            break
        // ============================================================
        // 以下是conversation页面 用到的回调
        // ============================================================
        // sdk 会话列表推送 ok
        case 'ConversationPushNotice':
            conversationHandle.conversationPushNoticeHandle(msg)
            break
        // sdk 好友列表 ok
        case 'FriendPushNotice':
            conversationHandle.friendPushNoticeHandle(msg)
            break
        // sdk 微信收消息通知 ok
        case 'FriendTalkNotice':
            conversationHandle.friendTalkNoticeHandle(msg)
            break
        // sdk 微信发信息通知 ok
        case 'WeChatTalkToFriendNotice':
            conversationHandle.weChatTalkToFriendNoticeHandle(msg)
            break
        // sdk 好友信息变更通知 ok
        case 'FriendAddNotice':
            conversationHandle.friendAddNoticeHandle(msg)
            break
        // sdk 发消息任务结果 ok
        case 'TalkToFriendTaskResultNotice':
            conversationHandle.talkToFriendTaskResultNoticeHandle(msg)
            break
        // sdk 好友删除通知 ok
        case 'FriendDelNotice':
            conversationHandle.friendDelNoticeHandle(msg)
            break
        // sdk 标签列表 ok
        case 'ContactLabelInfoNotice':
            previewHandle.contactLabelInfoNoticeHandle(msg)
            break
        // sdk 标签新增|修改通知 ok
        case 'ContactLabelAddNotice':
            conversationHandle.contactLabelAddNoticeHandle(msg)
            break
        // sdk 标签删除通知 ok
        case 'ContactLabelDelNotice':
            conversationHandle.contactLabelDelNoticeHandle(msg)
            break
        // 获取聊天详情 ok
        case 'RequestTalkDetailTaskResultNotice':
            conversationHandle.requestTalkDetailTaskResultNoticeHandle(msg)
            break
        // 打开红包或转账 ok
        case 'TakeMoneyTaskResultNotice':
            conversationHandle.takeMoneyTaskResultNoticeHandle(msg)
            break
        // 查询红包结果 ok
        case 'QueryHbDetailTaskResultNotice':
            conversationHandle.queryHbDetailTaskResultNoticeHandle(msg)
            break
        // 历史消息推送 ok
        case 'HistoryMsgPushNotice':
            conversationHandle.historyMsgPushNoticeHandle(msg)
            break
        // 手机消息已读通知 ok
        case 'PostMessageReadNotice':
            conversationHandle.postMessageReadNoticeHandle(msg)
            break
        // ============================================================
        // 以下是conversation群聊 用到的回调
        // ============================================================
        // 推送群聊列表 ok
        case 'ChatroomPushNotice':
            conversationHandle.chatroomPushNoticeHandle(msg)
            break
        // 群成员信息 ok
        case 'ChatRoomMembersNotice':
            conversationHandle.chatRoomMembersNoticeHandle(msg)
            break
        // 群聊新增通知 ok
        case 'ChatRoomAddNotice':
            conversationHandle.chatRoomAddNoticeHandle(msg)
            break
        // 群聊删除通知 ok
        case 'ChatRoomDelNotice':
            conversationHandle.chatRoomDelNoticeHandle(msg)
            break
        // 群聊信息变更通知 ok
        case 'ChatRoomChangedNotice':
            conversationHandle.chatRoomChangedNoticeHandle(msg)
            break
        // 群二维码 ok
        case 'PullChatRoomQrCodeTaskResultNotice':
            conversationHandle.pullChatRoomQrCodeTaskResultNoticeHandle(msg)
            break

        // ============================================================
        // 以下是任务处理结果的回调
        // ============================================================
        case 'TaskResultNotice':
            taskResultNotice.taskResultMap(msg)
            break
        // ============================================================
        // 以下是服务端的回调
        // ============================================================
        // server 返回
        case 'MsgReceivedAck':
            switch (msg.refMsgId) {
                case 1001:
                    // console.log('心跳返回。。。')
                    break
                default:
                    // console.log('服务端收到指令返回')
                    break
            }
            break
        // ============================================================
        // 以下是 错误用到的回调
        // ============================================================
        case 'Error':
            {
                let errMsg = JSON.parse(msg.message)
                let errorCode = errMsg.ErrorCode
                let errorMsg = errMsg.ErrorMsg
                if (errMsg.ErrorCode === 'NoRight' && errorMsg === "账号密码错误") {
                    store.commit('websocket/SET_LOGIN_LOADING', false)
                }
                // 单点登陆
                if (errMsg.ErrorCode === 'NoRight' && (errorMsg === '账号已在别处登陆' || errorMsg === 'token过期')) {
                    errorMsg = errorMsg + "，请重新登陆 !"
                    MessageBox.alert(
                        errorMsg,
                        errorCode,
                        {
                            type: 'error',
                            showClose: false,
                            confirmButtonText: '确定并退出',
                            callback: () => {
                                // 清除token
                                store.commit('SET_TOKEN', '')
                            }
                        }
                    )
                } else {
                    Message({
                        type: "error",
                        message: errorMsg,
                        duration: 2000,
                    })
                }
            }
            break
        // =========================================================================
        // ============================服务端的任务==================================
        // =========================================================================
        // server 退出登陆 ok
        case 'DeviceExitNotice':
            serverInfo.deviceExitNoticeHandle(msg)
            break
        // server 定时任务列表 ok
        case 'GetTaskInfoListResp':
            serverInfo.getTaskInfoListRespHandle(msg)
            break
        // server 批量加好友 ok
        case 'AutoFriendAddTaskResp':
            serverInfo.autoFriendAddTaskRespHandle(msg)
            break
        // server 批量加好友任务列表 ok
        case 'GetAutoFriendAddTaskListResp':
            serverInfo.getAutoFriendAddTaskListRespHandle(msg)
            break
        // server 号码列表 ok
        case 'GetPhoneNumberListResp':
            serverInfo.getPhoneNumberListResp(msg)
            break
        // server 公共话术 ok
        case 'GetCommonTermsResp':
            serverInfo.getCommonTermsRespHandle(msg)
            break
        // server  获取任务列表
        case 'GetTodoResp':
            serverInfo.getTodoHandle(msg)
            break
        // 获取任务明细列表
        case 'GetTaskDetailManagerResp':
            serverInfo.getTaskDetailManager(msg)
            break
            
        // 获取任务详情
        case 'CreateTaskDatilResp':
            serverInfo.createdatilHandle(msg)
            break
        // 已完成按钮
        case 'CloseTodoResp':
            serverInfo.checkendHandle(msg)
            break
        // 字典
        case 'GetAllDictResp':
            serverInfo.getdictlist(msg)
            break
        // 录入
        case 'SaveTaskDatilResp':
            serverInfo.savetask(msg)
            break
        //撤回任务
        case 'CloseTaskDetailResp':
            serverInfo.closetaskdetail(msg)
            break
        // 修改录入
        case 'UpdateTaskDetailResp':
            serverInfo.updatatask(msg)
            break
        // 通知列表
        case 'GetNoticeListResp':
            serverInfo.noticelHandle(msg)
            break
        // 通知已读
        case 'CloseNoticeListResp':
            serverInfo.closeNoticelHandle(msg)
            break
        // server 服务端定义的公共标签 ok
        case 'GetCommonTagsResp':
            serverInfo.getCommonTagsRespHandle(msg)
            break
        // server 历史消息
        case 'QueryHistoryMessageResp':
            //   serverInfo.queryHistoryMessageRespHandle(msg)
            break
        // server 历史消息
        case 'HistoryMessageListResp':
            //   serverInfo.historyMessageListRespHandle(msg)
            break
        // server 公众号列表推送 ok
        case 'BizContactPushNotice':
            // console.log('公众号列表推送:BizContactPushNotice')
            // store.commit('conversation/SET_GH_LIST', JSON.parse(msg.message))
            store.commit('conversation/SET_GH_MAP', JSON.parse(msg.message))
            break
        // 敏感词过滤
        case 'warning':
            {
                let warningMsg = JSON.parse(msg.message)
                Message({
                    type: 'warning',
                    message: warningMsg.msg,
                    duration: 2000
                })
                // 删除本地的数据
                store.dispatch('nedb/RemoveChatsByMsgId', warningMsg)
            }
            break
        // 商务列表
        // case 'GetBuinessListResp':
        //   serverInfo.getBuinessListRespHandle(msg)
        //   break
        // 上传好友的备注
        // case 'SaveFriendTagDetailResp':
        //   serverInfo.saveFriendTagDetailRespHandle(msg)
        //   break
        // 指定好友的备注
        // case 'GetWechatFriendDetailResp':
        //   serverInfo.getWechatFriendDetailRespHandle(msg)
        //   break
        default:
            break
    }
}
